package cn.tedu.project.oa.attendance.controller;

import cn.tedu.project.oa.attendance.pojo.dto.AttendanceQuery;
import cn.tedu.project.oa.attendance.pojo.dto.AttendanceSaveParam;
import cn.tedu.project.oa.attendance.pojo.entity.Attendance;
import cn.tedu.project.oa.attendance.pojo.vo.AttendanceInfoVO;
import cn.tedu.project.oa.attendance.pojo.vo.AttendanceVO;
import cn.tedu.project.oa.attendance.service.AttendanceService;
import cn.tedu.project.oa.base.response.JsonResult;
import cn.tedu.project.oa.base.webSocket.CustomWebSocketEndpoint;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import shade.com.alibaba.fastjson2.JSON;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@Api(tags = "03.每日考勤")
@RequestMapping("/oa/attendance")
public class AttendanceController {
    @Autowired
    private AttendanceService attendanceService;

    /**
     * 查询员工考勤列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询员工考勤列表")
    public JsonResult list(AttendanceQuery attendanceQuery)
    {
        List<AttendanceVO> list = attendanceService.selectAttendanceList(attendanceQuery);
        return JsonResult.ok(list);
    }
    /**
     * 获取员工考勤详细信息
     */
    @GetMapping(value = "/getInfo/{id}")
    @ApiOperation(value = "获取员工考勤详细信息")
    public JsonResult getInfo(@PathVariable Long id)
    {
        log.debug("getInfo(id={})", id);
        Attendance attendance = attendanceService.selectAttendanceById(id);
        return JsonResult.ok(attendance);
    }

    //按月查询考勤统计
    @GetMapping(value = "/get/statistics")
    @ApiOperation(value = "按月查询考勤统计")
    public JsonResult getStatistics(AttendanceQuery attendanceQuery)
    {
        log.debug("getStatistics(attendanceQuery={})", attendanceQuery);
        return JsonResult.ok();
    }

    //查询当日总签到数
    @GetMapping("count")
    @ApiOperation(value = "当日打卡数")
    public JsonResult countAllToday(){
        log.debug("今日打卡数");

        int num = attendanceService.count();

        return JsonResult.ok(num);
    }

    //TODO被Token拦截站内信测试
    @GetMapping("addNum")
    @ResponseBody
    public String sendMessage() {
        String userId = "1";
        Map<String,String> map = new HashMap<>();
        map.put("message","新消息");
        String message = "{message : '新消息'}";

        try {
            CustomWebSocketEndpoint.sendMessageToUser(userId, JSON.toJSONString(map));
            return "发送消息成功:" + userId;
        } catch (IOException e) {
            e.printStackTrace();
            return "发送消息失败";
        }
    }
}
